﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Diagnostics.Contracts;

namespace Common.Tests.Diagnostics.Contracts
{
    [TestFixture]
    public class ContractExTests
    {
        private const string STR_TestMessage = "TestMessage";


        #region Check<>(bool) tests
        [Test, ExpectedException(typeof(ArgumentNullException))]
        public void Check_throws_if_passed_condition_is_not_fulfilled()
        {
            ContractEx.Check<ArgumentNullException>(false);
        }

        [Test]
        public void Check_does_not_throw_if_passed_condition_is_fulfilled()
        {
            ContractEx.Check<ArgumentNullException>(true);
        }
        #endregion

        #region Check<>(bool) tests
        [Test, ExpectedException(typeof(Exception), ExpectedMessage=STR_TestMessage)]
        public void Check_with_msg_throws_if_passed_condition_is_not_fulfilled()
        {
            ContractEx.Check<Exception>(false, STR_TestMessage);
        }

        [Test]
        public void Check_with_msg_does_not_throw_if_passed_condition_is_fulfilled()
        {
            ContractEx.Check<ArgumentNullException>(true, STR_TestMessage);
        }

        [Test, ExpectedException(typeof(ParameterlessException))]
        public void Check_with_msg_throws_even_no_message_can_be_passed_to_CTOR()
        {
            ContractEx.Check<ParameterlessException>(false, STR_TestMessage);
        }
        #endregion

        #region Check with Func<bool> tests
        [Test, ExpectedException(typeof(Exception), ExpectedMessage=STR_TestMessage)]
        public void Check_with_func_throws()
        {
            Func<bool> f = () => { return false; };
            ContractEx.Check<Exception>(f, STR_TestMessage);
        }
        #endregion

        class ParameterlessException : Exception
        {
        }
    }
}
